package com.javaee.my.business;

import com.javaee.interceptor.entity.Shopping_Cart;
import com.javaee.interceptor.entity.goods;
import com.javaee.my.user.BaseControllerApi;
import org.apache.commons.lang.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/superMarket")
public class shoppingController extends HttpServlet implements BaseControllerApi {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        Integer nums= Integer.valueOf(req.getParameter("nums"));//获取商品数目
        Integer goods_id = Integer.valueOf(req.getParameter("goods_id"));//获取商品的id
        Integer id= (Integer) req.getSession().getAttribute("id");//以账号id为标识符;
        Shopping_Cart shopping_cart=new Shopping_Cart();//创建一个购物车对象
        shopping_cart.setId(id);
        shopping_cart.setGoods_id(goods_id);//将信息封装到对象中
        //0.先核实商品信息
        goods resGoods = sqlsession.selectOne("com.javaee.my.dao.businessMapper.check_goods", goods_id);//查看商品栏中是否有商品
        if (resGoods==null||resGoods.getGoods_nums()<nums)//没有直接转发到购物车界面
        {
            System.out.println("商品信息填写有错误！");
            req.getRequestDispatcher("superMarket.jsp").forward(req,resp);
        }else {
            //1.判断购物车中是否有需要添加的商品
            shopping_cart = sqlsession.selectOne("com.javaee.my.dao.businessMapper.check_goodsInShopping_cart", shopping_cart);
            if (shopping_cart!=null)//如果有,则更新商品中的总价格和数量
            {
                shopping_cart.setNums(shopping_cart.getNums()+nums);
                shopping_cart.setAllprice(shopping_cart.getNums()*resGoods.getGoods_price());
                Integer res=sqlsession.update("com.javaee.my.dao.businessMapper.update_goodsInShopping_cart",
                        shopping_cart);//更新商品数量
                //2.判断放入数据是否失败
                if (StringUtils.isBlank(String.valueOf(res))) {
                    System.out.println("数据更新失败");
                    req.getRequestDispatcher("superMarket.jsp").forward(req, resp);
                }
                sqlsession.commit();
            }else {//如果购物车中预先没有需要添加的商品
                //创建购物车对象
                Shopping_Cart shopping_cart2 = new Shopping_Cart(0, goods_id, id, nums, nums * resGoods.getGoods_price());
                int res = sqlsession.insert("com.javaee.my.dao.businessMapper.addGoodsToShopping_cart", shopping_cart2);
                //2.判断放入数据是否失败
                if (StringUtils.isBlank(String.valueOf(res))) {
                    req.getRequestDispatcher("superMarket.jsp").forward(req, resp);
                }
                sqlsession.commit();
            }
            resGoods.setGoods_nums(resGoods.getGoods_nums() - nums);
            //3.更改商品栏上的数据
            sqlsession.update("com.javaee.my.dao.businessMapper.update_goodsNums",resGoods);
            sqlsession.commit();
            //4.回显到superMarket.jsp
            req.getRequestDispatcher("superMarket.jsp").forward(req, resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }
}
